package com.easyban.controller;

import java.util.List;

import com.easyban.Dto.FileInfoDto;
import com.easyban.Dto.SessionWebUserDto;
import com.easyban.annotation.GlobalInterceptor;
import com.easyban.annotation.VerifyParam;
import com.easyban.entity.enums.FileCategoryEnums;
import com.easyban.entity.enums.FileCodeEunm;
import com.easyban.entity.query.FileInfoQuery;
import com.easyban.entity.po.FileInfo;
import com.easyban.entity.vo.FileInfoVo;
import com.easyban.entity.vo.PaginationResultVO;
import com.easyban.entity.vo.ResponseVO;
import com.easyban.entity.vo.UploadResultDto;
import com.easyban.exception.BusinessException;
import com.easyban.service.FileInfoService;
import com.easyban.utils.CopyTools;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 文件信息 Controller
 */
@RestController("fileInfoController")
@RequestMapping("/file")
public class FileInfoController extends CommonFileController{

	@Resource
	private FileInfoService fileInfoService;
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/loadDataList")
	@GlobalInterceptor(checkLogin = true)
	public ResponseVO loadDataList(HttpSession session, FileInfoQuery query,String category){
		FileCategoryEnums bycategory = FileCategoryEnums.getBycategory(category);
		if (bycategory!=null){
			query.setFileCategory(bycategory.getCategory());
		}
		SessionWebUserDto sessionV = getSessionV(session);
		query.setUserId(sessionV.getUserId());
		query.setOrderBy("last_update_time desc");
		query.setDelFlag(FileCodeEunm.USING.getFlag());
		PaginationResultVO<FileInfo> listByPage = fileInfoService.findListByPage(query);
		return getSuccessResponseVO(convert2PaginationVO(listByPage, FileInfoVo.class));
	}
	@RequestMapping("/uploadFile")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO uploadFile(HttpSession session,
								 FileInfoDto fileInfoDto
								 ){
		System.out.println(fileInfoDto.toString());
		SessionWebUserDto sessionV = getSessionV(session);
		UploadResultDto resultDto = fileInfoService.uploadFile(sessionV,fileInfoDto);
        return getSuccessResponseVO(resultDto);
	}
	@RequestMapping("/getImage/{imageFolder}/{imageName}")
	public void getImage(HttpServletResponse response, @PathVariable("imageFolder") String imageFolder, @PathVariable("imageName") String imageName) {
		super.getImage(response, imageFolder, imageName);
	}
	@RequestMapping("/ts/getVideoInfo/{fileId}")
	@GlobalInterceptor(checkLogin = true)
	public void getVideoInfo(HttpServletResponse response, HttpSession session, @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
		SessionWebUserDto webUserDto = getSessionV(session);
		super.getFile(response, fileId, webUserDto.getUserId());
	}
	@RequestMapping("/getFile/{fileId}")
	@GlobalInterceptor(checkLogin = true)
	public void getoInfo(HttpServletResponse response, HttpSession session, @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
		SessionWebUserDto webUserDto = getSessionV(session);
		super.getFile(response, fileId, webUserDto.getUserId());
	}
	@RequestMapping("/newFoloder")
	@GlobalInterceptor(checkLogin = true)
	public ResponseVO newFolder(HttpServletResponse response, HttpSession session,
						  @VerifyParam(required = true) String filePid,
						  @VerifyParam(required = true) String fileName) {
		SessionWebUserDto webUserDto = getSessionV(session);
		if (SensitiveWordHelper.contains(fileName)){
			throw new BusinessException("文件名含有敏感词");
		}

			FileInfo fileInfo = fileInfoService.newFolder(filePid, webUserDto.getUserId(), fileName);
		return getSuccessResponseVO(fileInfo);
	}
	@RequestMapping("/getFolderInfo")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO getFolderInfo(HttpSession session, @VerifyParam(required = true) String path) {
		return super.getFolderInfo(path, getSessionV(session).getUserId());
	}
	@RequestMapping("/loadAllFolder")
	@GlobalInterceptor(checkParams = true,checkLogin = true)
	public ResponseVO loadAllFolder(HttpSession session, @VerifyParam(required = true) String filePid, String currentFileIds) {
		List<FileInfo> fileInfoList=   fileInfoService.findListByParamAndExclude(filePid,currentFileIds);
		return getSuccessResponseVO(CopyTools.copyList(fileInfoList, FileInfoVo.class));
	}
	/*移动文件*/
	@RequestMapping("/changeFileFolder")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO changeFileFolder(HttpSession session,
									   @VerifyParam(required = true) String fileIds,
									   @VerifyParam(required = true) String filePid) {
		SessionWebUserDto webUserDto = getSessionV(session);
		fileInfoService.changeFileFolder(fileIds, filePid, webUserDto.getUserId());
		return getSuccessResponseVO(null);
	}
	@RequestMapping("/createDownloadUrl/{fileId}")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO createDownloadUrl(HttpSession session, @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
		return super.createDownloadUrl(fileId, getSessionV(session).getUserId());
	}

	@RequestMapping("/download/{code}")
	@GlobalInterceptor(checkLogin = false, checkParams = true)
	public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("code") @VerifyParam(required = true) String code) throws Exception {
		super.download(request, response, code);
	}
	@RequestMapping("/delFile")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO delFile(HttpSession session, @VerifyParam(required = true) String fileIds) {
		SessionWebUserDto webUserDto = getSessionV(session);
		fileInfoService.removeFile2RecycleBatch(webUserDto.getUserId(), fileIds);
		return getSuccessResponseVO(null);
	}

	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(FileInfo bean) {
		fileInfoService.add(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<FileInfo> listBean) {
		fileInfoService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<FileInfo> listBean) {
		fileInfoService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据FileIdAndUserId查询对象
	 */
	@RequestMapping("/getFileInfoByFileIdAndUserId")
	public ResponseVO getFileInfoByFileIdAndUserId(String fileId,String userId) {
		return getSuccessResponseVO(fileInfoService.getFileInfoByFileIdAndUserId(fileId,userId));
	}
	@RequestMapping("/rename")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO rename(HttpSession session,
							 @VerifyParam(required = true) String fileId,
							 @VerifyParam(required = true) String fileName) {
		SessionWebUserDto webUserDto = getSessionV(session);
		FileInfo fileInfo = fileInfoService.rename(fileId, webUserDto.getUserId(), fileName);
		return getSuccessResponseVO(CopyTools.copy(fileInfo, FileInfoVo.class));
	}
	/**
	 * 根据FileIdAndUserId修改对象
	 */
	@RequestMapping("/updateFileInfoByFileIdAndUserId")
	public ResponseVO updateFileInfoByFileIdAndUserId(FileInfo bean,String fileId,String userId) {
		fileInfoService.updateFileInfoByFileIdAndUserId(bean,fileId,userId);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据FileIdAndUserId删除
	 */
	@RequestMapping("/deleteFileInfoByFileIdAndUserId")
	public ResponseVO deleteFileInfoByFileIdAndUserId(String fileId,String userId) {
		fileInfoService.deleteFileInfoByFileIdAndUserId(fileId,userId);
		return getSuccessResponseVO(null);
	}
}